/*
 * Copyright (c) 2021 HiSilicon (Shanghai) Technologies CO., LIMITED.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <signal.h>
#include <unistd.h>
#include <fcntl.h>

#include <securec.h>
#include <mpi_sys.h>

#define LEN_NUM 2
/**
    二进制数据转换为hex表示的string.
*/
static int BinToStr(const void* bin, int binLen, char* str, int strSize)
{
    assert(strSize > binLen * LEN_NUM);
    int len = 0;

    *str = 0;
    for (int i = 0; i < binLen; i++) {
        len += snprintf_s(str + len, strSize - len, strSize - len - 1, "%02X", (int)((uint8_t*)bin)[i]);
        assert(len < strSize);
    }
    assert(len == binLen * LEN_NUM);
    return len;
}

/**
    app entry.
    不需要特别处理终止信号，进程因信号而终止时，exit code能反映它.
*/
int main(int argc, char *argv[])
{
    HI_UNIQUE_ID_S uniqueId = { 0 };
    char chipId[sizeof(uniqueId) * LEN_NUM + 1];
    int ret;

    ret = HI_MPI_SYS_GetUniqueId(&uniqueId);
    if (ret) {
        fprintf(stderr, "GetUniqueId FAIL, ret=%x\n", ret);
        return ENOENT;
    }

    BinToStr(&uniqueId, sizeof(uniqueId), chipId, sizeof(chipId));
    printf("%s\n", chipId);
    return 0;
}

